﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WinNemoLib;
using System.Data.SqlClient;
using System.Text;
using System.Data;

namespace Bias_Stock.Models
{
    public class MasterModel
    {
        public class CartMast_DS
        {
            public Int32 _Code { get; set; }
            public string _Type { get; set; }
            public string _NotUse { get; set; }
            public string _ModifyBy { get; set; }
            public string _ModifyDT { get; set; }
        }

        public List<CartMast_DS> SEL_Cart_MASTER(string code,bool notuse = false)
        {

            string ConnectionString = Nemo.GetConfiguredDbConnectionWrappertostring("Database");
          
            SqlConnection cnn = new SqlConnection(ConnectionString);
            StringBuilder str = new StringBuilder();
            string wh = " where ";
            str.AppendLine(" select * from " + Nemo.GetINI("Dbase", "Base") + ".dbo.CartMaster  ");
            if (StrUtil.IsNotEmpty(code))
            {
                str.Append(wh + "  cart_no = '" + code + "'");
                wh = " and ";
            }
            if (notuse)
            {
                str.Append(wh + " cart_notuse is null");
                wh = " and ";
            }


            SqlCommand cmd = new SqlCommand(str.ToString() + " order by convert(int,cart_no)", cnn);
            cmd.Connection.Open();
            cmd.CommandType = CommandType.Text;


            SqlDataAdapter my = new SqlDataAdapter();
            DataSet ds = new DataSet();
            my.SelectCommand = cmd;
            my.Fill(ds);
            List<CartMast_DS> list = new List<CartMast_DS>();
            if (ds.Tables[0].Rows.Count <= 0)
            {
                cmd.Connection.Close();
                return list;
            }
            CartMast_DS model = new CartMast_DS();
            for (int cuIndex = 0; cuIndex < ds.Tables[0].Rows.Count; cuIndex++)
            {
                DataRow items = ds.Tables[0].Rows[cuIndex];
                 model = new CartMast_DS();
                 model._Code = Convert.ToInt32(items["cart_no"]);
                model._Type = items["cart_type"].ToString().ToUpper();
                model._NotUse = items["Cart_NotUse"].ToString();
                model._ModifyBy = items["ModifyBy"].ToString();
                model._ModifyDT = DateUtil.ToDisplayDateTime(items["ModifyDT"], DateUtil.eLanguage.EN);

                list.Add(model);
            }
           
            return list;
        }

        public Boolean Ins_Data_Cart(ConnectionWrapper cnn, CartMast_DS incoming)
        {
            try
            {
                StringBuilder comstr = new StringBuilder();
                comstr.AppendLine(" INSERT INTO ");
                comstr.AppendLine(WinNemoLib.Nemo.GetINI("Dbase", "Base") + ".[dbo].[CartMaster]");
                comstr.AppendLine(" (cart_no,cart_type,Cart_NotUse,ModifyBy,ModifyDT) VALUES (");

                comstr.AppendLine(" @cart_no,@cart_type,@Cart_NotUse,@ModifyBy,getdate() ) ");

                CommandWrapper cmd = cnn.CreateCommand();
                cmd.CommandText = comstr.ToString();


                cmd.AddParameterStringWithValue("@cart_no", incoming._Code.ToString());

                cmd.AddParameterStringWithValue("@cart_type", incoming._Type.ToUpper());
                cmd.AddParameterStringWithValue("@Cart_NotUse", incoming._NotUse);
                cmd.AddParameterStringWithValue("@ModifyBy", incoming._ModifyBy);

                if (cmd.ExecuteNonQuery() <= 0)
                {
                    cnn.RollbackTransaction();
                    return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }

        }

        public Boolean Edit_Data_Cart(ConnectionWrapper cnn, CartMast_DS incoming)
        {
            try
            {
                StringBuilder comstr = new StringBuilder();
                comstr.AppendLine(" UPDATE ");
                comstr.AppendLine(WinNemoLib.Nemo.GetINI("Dbase", "Base") + ".[dbo].[CartMaster]");
                comstr.AppendLine(" set");

                comstr.AppendLine("cart_type=@cart_type,Cart_NotUse=@Cart_NotUse,ModifyBy=@ModifyBy,ModifyDT=getdate()  ");
                comstr.AppendLine(" where cart_no = @cart_no");
                CommandWrapper cmd = cnn.CreateCommand();
                cmd.CommandText = comstr.ToString();


                cmd.AddParameterStringWithValue("@cart_no", incoming._Code.ToString());

                cmd.AddParameterStringWithValue("@cart_type", incoming._Type.ToUpper());
                cmd.AddParameterStringWithValue("@Cart_NotUse", incoming._NotUse);
                cmd.AddParameterStringWithValue("@ModifyBy", incoming._ModifyBy);

                if (cmd.ExecuteNonQuery() <= 0)
                {
                    cnn.RollbackTransaction();
                    return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }

        }

    }
}